package com.china08.yxyapi.api.manage.tec.teaching.lecture.teachreport;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.esbag.YxyEsbagTeachingNoteTaking;
import com.china08.yxyapi.entity.mg.esbag.YxyEsbagTeachingNoteTaking.Note;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.esbag.YxyEsbagTeachingNoteTakingRepository;

/*** 老师---备授课--授课报告-随堂笔记 **/

@CrossOrigin
@RestController
public class LectureReportClassNotesController extends YxyApiBaseController {

    @Autowired
    private YxyEsbagTeachingNoteTakingRepository yxyEsbagTeachingNoteTakingRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/testreport/class/notes/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String id = reqModel.id;
        String classId = reqModel.classId;
        Assert.notNull(StringUtils.defaultIfBlank(id, null), "授课包ID为空");
        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        String userNick = getProfile(request, response).getUserNick();
        Integer page = reqModel.page;
        YxyEsbagTeachingNoteTaking noteTaking = yxyEsbagTeachingNoteTakingRepository
                .findByTeacherIdAndSchoolIdAndPackIdAndClassIdOrderByCreatedDate(userId, schoolId, id, classId);

        RespModel respModel = new RespModel();

        List<String> notList = new ArrayList<String>();
        List<String> noteTaking2 = new ArrayList<String>();
        if (null != noteTaking) {
            respModel.avantar = noteTaking.getAvantar();

            List<Note> taking = noteTaking.getNoteTaking();
            for (Note note : taking) {
                if (null != note.url) {
                    noteTaking2.add(note.url);
                }
            }
            respModel.allNoteTaking = noteTaking2;
            int size = 35;
            int size2 = noteTaking2.size();
            Integer total = size * page;
            if (total > size2) {
                total = size2;
            }
            notList.addAll(noteTaking2.subList((size * page) - 35, total));

            respModel.teacherNick = noteTaking.getTeacherNick();
            respModel.noteTaking = notList;
        } else {
            respModel.avantar = "";
            respModel.allNoteTaking = noteTaking2;
            respModel.teacherNick = userNick;
            respModel.noteTaking = notList;
        }

        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 授课包id
        public String id;
        // 班级id
        public String classId;
        // 当前页数
        public Integer page;
    }

    protected static class RespModel extends YxyApiRespModel {

        // 教师头像
        public String avantar;
        // 教师姓名
        public String teacherNick;
        // 课堂板书
        public List<String> noteTaking;
        // 课堂板书
        public List<String> allNoteTaking;
    }
}
